const express = require('express')
const userRouter = express.Router()
const fs = require('fs')

userRouter.get('/getuser',(req,res)=>{
    let userData = require('../data/userData.json')
    res.json({
        status: 0, message: '获取图书成功', data: userData 
    })
})
userRouter.post('/addact',(req,res)=>{
    let userData =  require('../data/userData.json')
    userData.act.push(req.body)
    fs.writeFile('./data/userData.json', JSON.stringify(userData),(error,data)=>{
        res.send({ status: 0, message: '添加成功' })
        })
})
userRouter.post('/addorder',(req,res)=>{
    let userData =  require('../data/userData.json')
    userData.order.push(req.body)
    fs.writeFile('./data/userData.json', JSON.stringify(userData),(error,data)=>{
        res.send({ status: 0, message: '添加成功' })
        })
})
userRouter.post('/addact',(req,res)=>{
    let userData =  require('../data/userData.json')
    userData.act.push(req.body)
    fs.writeFile('./data/userData.json', JSON.stringify(userData),(error,data)=>{
        res.send({ status: 0, message: '添加成功' })
        })
})
userRouter.post('/delorder',(req,res)=>{
    let userData =  require('../data/userData.json')
    userData.order.splice(req.body.index,1)
    fs.writeFile('./data/userData.json', JSON.stringify(userData),(error,data)=>{
        res.send({ status: 0, message: '添加成功' })
        })
})
userRouter.post('/delact',(req,res)=>{
    let userData =  require('../data/userData.json')
    userData.act.splice(req.body.index,1)
    fs.writeFile('./data/userData.json', JSON.stringify(userData),(error,data)=>{
        res.send({ status: 0, message: '添加成功' })
        })
})
module.exports = userRouter
